package com.itheima03;

import java.util.Arrays;

/**
 * @author Xiao
 * @date 2021/7/13
 */
public class ArrayDemo {
    /**
     * API_Arrays：冒泡排序
     */
    public static void main(String[] args) {
        int[] arr = {11, 33, 55, 77, 99, 22, 44, 66, 88};
        printArray(arr);
        //外循环：冒泡排序次数（arr.length - 1）
        for (int i = 0; i < arr.length-1; i++) {
            //内循环：遍历数组将最大值移到最右边，每遍历一次移动距离-1
            for (int j = 0; j < arr.length - 1 - i; j++) {
                changeMax(arr, j);
//                if (arr[j] > arr[j + 1]) {
//                    int temp = arr[j];
//                    arr[j] = arr[j + 1];
//                    arr[j + 1] = temp;
//                }
            }
        }
//        printArray(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void changeMax(int[] arr, int i) {
        if (arr[i] > arr[i+1]) {
            int temp = arr[i];
            arr[i] = arr[i+1];
            arr[i+1] = temp;
        }
    }

    private static void printArray(int[] arr) {
        System.out.print("[");
        for (int i = 0; i < arr.length; i++) {
            if (i == arr.length - 1) {
                System.out.print(arr[i]);
            } else {
                System.out.print(arr[i] + ", ");
            }
        }
        System.out.println("]");
    }
}
